package com.mblog.util;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.web.multipart.MultipartFile;

public class FileUpload {
	/**
	 * 
	 * @param upload 上传的文件
	 * @param name  文件名字包括后缀
	 * @param allPath 是否获取文件绝对路径
	 * @param baseUploadPath
	 * @return
	 */
	public static String saveUploadFile(MultipartFile upload,String baseUploadPath) {
		String fileName=new SimpleDateFormat("yyyyMMddHHmmssSSSS").format(new Date())+upload.getOriginalFilename().substring(upload.getOriginalFilename().lastIndexOf("."));
		File directoryFile=new File(baseUploadPath);
		if  (!directoryFile .exists()  && !directoryFile .isDirectory())      
		{       
			//创建文件夹
		    directoryFile .mkdir();
		}
		File f=new File(baseUploadPath,fileName);
		try {
			upload.transferTo(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fileName;
	}


}
